﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Bill_IncomeRecord : System.Web.UI.Page
{
    //private Order _CurrentOrder;
    ///// <summary>
    ///// 当前页面所拥有的订单实体
    ///// </summary>
    //protected Order CurrentOrder
    //{
    //    get
    //    {
    //        if (_CurrentOrder == null)
    //        {
    //            if (Request.QueryString["OrderId"] == null || Request.QueryString["OrderId"].Length == 0)
    //            {
    //                Response.Redirect("~/Finance", true);
    //            }
    //            int orderId = Convert.ToInt32(Request.QueryString["OrderId"]);
    //            _CurrentOrder = Order.Load(orderId);
    //            if (_CurrentOrder == null)
    //            {
    //                Response.Redirect("~/Finance", true);
    //            }
    //        }
    //        return _CurrentOrder;
    //    }
    //}

    //#region 获取对应IncomeRecordId的收款单信息
    ///// <summary>
    ///// 获取对应IncomeRecordId的收款单信息
    ///// </summary>
    //private void GetAndBindIncomeRecordByIncomeRecordId()
    //{
    //    IncomeRecord icr = IncomeRecord.GetIncomeRecord(int.Parse(Request.QueryString["IncomeRecordId"]));
    //    this.imgBillAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", icr.Order.Bill.FinanceAudit.ToString());
    //    this.imgCustomerAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", icr.Order.FinanceAudit.ToString());
    //    this.lblOrderId.Text = icr.OrderId.ToString();
    //    this.txtDescription.Text = icr.Description.ToString();
    //    this.txtIncome.Text = icr.Income.ToString();
    //    this.txtIncomeDate.Text = icr.IncomeDate.ToString("yyyy-MM-dd");
    //    this.lblIncomeUnit.ToolTip = icr.IncomeUnitId.ToString();
    //    this.lblIncomeUnit.Text = icr.Order.Bill.Customer.Name.ToString();
    //    decimal finalRealIncome = 0;
    //    if (icr.Order.CustomerOrders != null)
    //    {
    //        foreach (CustomerOrder co in icr.Order.CustomerOrders)
    //        {
    //            finalRealIncome += co.RealPayout;
    //        }
    //    }
    //    this.lblFinalRealIncome.Text = finalRealIncome.ToString("c");
    //    this.lblMediaName.Text = icr.Order.MediaInfo.Name;
    //    decimal alreadyIncome = 0;
    //    foreach (IncomeRecord ir in icr.Order.IncomeRecords)
    //    {
    //        alreadyIncome += ir.Income;
    //    }
    //    this.lblAlreadyIncome.Text = alreadyIncome.ToString("c");
    //    icr = null;
    //}
    //#endregion

    ///// <summary>
    ///// 页面首次加载时，获取定单号和相关信息
    ///// </summary>
    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    if (!IsPostBack)
    //    {
    //        if (Request.UrlReferrer == null)
    //        {
    //            this.Response.Write("请不要非法进入本页！<br/>3秒后将转回....");
    //            this.Response.Write("<script type=\"text/javascript\">setTimeout(\"location.href='../Finance/'\",3*1000);</script>");
    //            this.Response.End();
    //            return;
    //        }
    //        else
    //        {
    //            if (Request.QueryString["IncomeRecordId"] != null || Request.QueryString["OrderId"] != null)
    //            {
    //                if (!(Request.QueryString["OrderId"] == null && Request.QueryString["IncomeRecordId"] == null))
    //                {
    //                    if (Request.QueryString["IncomeRecordId"] != null)  //如果传过来的IncomeRecordId不为空，则显示修改收款单
    //                    {
    //                        this.btnNew.Visible = true;
    //                        this.lblTitle.InnerHtml = "修改收款单";
    //                        this.btnSaveIncomeRecord.Text = "确认修改";
    //                        IncomeRecord icr = 
    //                            IncomeRecord.GetIncomeRecord(int.Parse(Request.QueryString["IncomeRecordId"]));
    //                        GetAndBindIncomeRecordByIncomeRecordId();
    //                        IncomeRecord[] irs = icr.Order.IncomeRecords;
    //                        this.IncomeRecordsGridView.DataSource = irs;
    //                        this.IncomeRecordsGridView.DataBind();
    //                    }
    //                    else
    //                    {
    //                        this.txtIncomeDate.Text = DateTime.Now.ToShortDateString();
    //                        this.btnNew.Visible = false;
    //                        this.lblOrderId.Text = Request.QueryString["OrderId"];
    //                        //this.lblListName.Text = "该订单的收款记录";
    //                        Order odr = Order.Load(int.Parse(Request.QueryString["OrderId"]));
    //                        if (odr.FinanceAudit == Audit.Success || odr.Bill.FinanceAudit == Audit.Success)
    //                        {
    //                            this.IncomeRecordEditFieldset.Visible = false;
    //                            this.IncomeRecordsGridView.Columns[5].Visible = false;
    //                            this.IncomeRecordsGridView.Columns[6].Visible = false;
    //                        }
    //                        this.imgBillAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", odr.Bill.FinanceAudit.ToString());
    //                        this.imgCustomerAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", odr.FinanceAudit.ToString());
    //                        this.lblIncomeUnit.ToolTip = odr.Bill.Customer.Id.ToString(); //用于保存收款单位的ID
    //                        decimal finalRealIncome = 0;
    //                        if (odr.CustomerOrders != null)
    //                        {
    //                            foreach (CustomerOrder co in odr.CustomerOrders)
    //                            {
    //                                finalRealIncome += co.RealPayout;
    //                            }
    //                        }
    //                        this.lblFinalRealIncome.Text = finalRealIncome.ToString("c");
    //                        this.lblMediaName.Text = odr.MediaInfo.Name;
    //                        decimal totalAlreadyIncome = 0;
    //                        if (odr.IncomeRecords != null)
    //                        {
    //                            foreach (IncomeRecord ir in odr.IncomeRecords)
    //                            {
    //                                totalAlreadyIncome += ir.Income;
    //                            }
    //                        }
    //                        lblAlreadyIncome.Text = totalAlreadyIncome.ToString("c");
    //                        this.lblIncomeUnit.Text = odr.Bill.Customer.Name;
    //                        //this.IncomeRecordsGridView.DataSourceID = "odsSelectIncomeRecordByOrderId";
    //                        this.IncomeRecordsGridView.DataSource = CurrentOrder.IncomeRecords;
    //                        this.IncomeRecordsGridView.DataBind();
    //                    }
    //                }
    //            }
    //            else
    //            {
    //                Response.Redirect("../Finance/", true);
    //            }
    //        }
    //    }
    //}

    //protected override void OnPreRenderComplete(EventArgs e)
    //{
    //    base.OnPreRenderComplete(e);
    //    if (CurrentOrder.Bill.FinanceAudit == Audit.Success)
    //    {
    //        this.IncomeRecordEditFieldset.Visible = false;
    //    }
    //}

    //protected void btnSaveIncomeRecord_Click(object sender, EventArgs e)
    //{
    //    IncomeRecord icr;
    //    if (btnSaveIncomeRecord.Text == "确认收款")
    //    {
    //        icr = new IncomeRecord();
    //        icr.OrderId = Convert.ToInt16(lblOrderId.Text);
    //        icr.Income = Convert.ToDecimal(txtIncome.Text);
    //        icr.IncomeDate = Convert.ToDateTime(txtIncomeDate.Text);
    //        icr.IncomeUnitId = Convert.ToInt16(lblIncomeUnit.ToolTip);
    //        icr.Description = txtDescription.Text;
    //        Order odr = icr.Order;
    //        //icr.Save();
    //        odr.SaveIncomeRecord(icr, icr.IncomeDate);
    //        Response.Redirect("IncomeRecord.aspx?OrderId=" + icr.OrderId );
    //    }
    //    else
    //    {
    //        icr = IncomeRecord.Load(int.Parse(Request.QueryString["IncomeRecordId"]));

    //        icr.OrderId = Convert.ToInt16(this.lblOrderId.Text);
    //        icr.Income = Convert.ToDecimal(this.txtIncome.Text);
    //        icr.IncomeDate = Convert.ToDateTime(this.txtIncomeDate.Text);
    //        //icr.IncomeUnitId = Convert.ToInt16(this.lblIncomeUnit.AccessKey);
    //        icr.IncomeUnitId = Convert.ToInt16(this.lblIncomeUnit.ToolTip.ToString());
    //        icr.Description = this.txtDescription.Text;
    //        Order odr = icr.Order;
    //        //icr.Save();
    //        odr.SaveIncomeRecord(icr, icr.IncomeDate);
    //        Response.Redirect("IncomeRecord.aspx?OrderId=" + icr.OrderId);
    //    }
    //}
    //protected void IncomeRecordsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        Label lblIncomeUnit = (Label)e.Row.FindControl("lblIncomeUnit");
    //        //if (this.IncomeRecordsGridView.DataKeys.Count > 0)
    //        //for (int i = 0; i < this.GridView1.DataKeys.Count; i++)
    //        //{
    //        lblIncomeUnit.Text = CurrentOrder.Bill.Customer.Name;
    //                //IncomeRecord.GetIncomeRecord(Convert.ToInt16(this.IncomeRecordsGridView.DataKeys[e.Row.RowIndex].Values[0].ToString())).Order.Bill.Customer.Name;
    //            //lblIncomeUnit.Text = Order.Load(Convert.ToInt16(this.GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString())).Bill.Customer.Name;
    //        //}
    //    }
    //}
    //protected void IncomeRecordsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int Id = Convert.ToInt16(this.IncomeRecordsGridView.DataKeys[e.RowIndex].Values[0].ToString());
    //    IncomeRecord ir = IncomeRecord.GetIncomeRecord(Id);
    //    //ir.Delete();
    //    //ir.Save();
    //    Order odr = ir.Order;
    //    odr.SaveIncomeRecord(ir, ir.IncomeDate);
    //    Response.Redirect("IncomeRecord.aspx?OrderId=" + ir.OrderId);
    //}

    //protected void btnNew_Click(object sender, EventArgs e)
    //{
    //    IncomeRecord icr = IncomeRecord.GetIncomeRecord(int.Parse(Request.QueryString["IncomeRecordId"]));

    //    Response.Redirect("IncomeRecord.aspx?OrderId=" + icr.OrderId);
    //}

    //protected void btnBack_Click(object sender, EventArgs e)
    //{
    //    if (Request.QueryString["IncomeRecordId"] != null)
    //    {
    //        IncomeRecord icr = IncomeRecord.GetIncomeRecord(int.Parse(Request.QueryString["IncomeRecordId"]));
    //        Response.Redirect("~/Finance/Accounts.aspx?BillId=" + icr.Order.BillId);
    //    }
    //    else if (Request.QueryString["OrderId"] != null)
    //    {
    //        Order odr = Order.Load(int.Parse(Request.QueryString["OrderId"]));
    //        Response.Redirect("~/Finance/Accounts.aspx?BillId=" + odr.BillId);
    //    }
    //}
}
